const express =require('express')
var md5 =require('md5')

const adminModel = require('../db/model/adminModel');

const route =express.Router()
//引入正则校验中间件
const addAminReg = require('../middleware/addAdminReg')
//引入权限中间件
const isLoginMiddle = require("../middleware/isLoginMiddle")

route.post('/addAdmin',addAminReg,isLoginMiddle,async (req,res)=>{
    const {
        adminName,
        password,
        regTime
    } = req.body;
    // console.log(req.body);
    try {
        //拿着用户数据在admin中查询是否存在
        const findResult = await adminModel.findOne({
            adminName,
        })
        //如果有找到说明 用户已经被注册
        if (findResult) {
            return res.send({
                code: 10001,
                msg: "添加管理员-用户已经被注册",
                data: null
            })
        }
        //如果没有查询到就添加
        await adminModel.create({
            adminName,
            password :md5(password + '123'),
            regTime
        });
        res.send({
            code: 10000,
            msg: "添加管理员-添加成功",
            data: null
        })
    } catch (e) {
        console.log("添加管理员中出现异常"+ e.message);
        res.send({
            code: 10002,
            msg: "添加管理员-服务端异常",
            data: null
        })
        }


})
module.exports = route